//
//  MKCellTopView.swift
//  Weibo
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 MK. All rights reserved.
//


/*
需要把图片视图 放在topView的下面
这里的topView包含cellImageView
*/


import UIKit
import SDWebImage
import SnapKit

class MKCellTopView: UIView {
    
    //0.定义底部约束
    var bottomConstraint: Constraint?
    
    var staModel: MKStatusModel? {
        didSet {
            //给子视图赋值
            iconImage.sd_setImageWithURL(staModel?.user?.iconImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            iconImage.layer.cornerRadius = CGFloat(Double(cellIconWidth) * 0.5)
            iconImage.layer.masksToBounds = true
            nameLabel.text = staModel?.user?.name
            vipImage.image = staModel?.user?.mbrankImage
            verifiImage.image = staModel?.user?.verified_type_image
            timeLabel.text = staModel?.created_at
            sourceLabel.text = staModel?.source
            textFile.text = staModel?.text
            
            //给图片视图传递url数组
            pictureView.urlArr = staModel?.pic_nsurls
            
            //2.干掉之前记录的约束
            bottomConstraint?.uninstall()
            
            //FIXME: - 根据是否有图片 来动态的调整约束 就可解决没有图片的地方去掉margin
            if let urls = staModel?.pic_nsurls where urls.count > 0 {
                
                //有配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    //1.记录约束对象
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
            } else {
                
                //无配图 - 1记录约束对象 2.接着清除之前的约束 3.再更新约束
                //#119 // Weibo.MKCellTopView.bottom == Weibo.MKPictureView:.bottom + 12.0
                //#43 // Weibo.MKCellTopView.bottom == UILabel + 12.0
                //更新约束的前提 需要参照的对象和约束的方向一致 不然后台会打印好多错误 -- 解决办法 把之前的约束给干掉 --步骤 先记录下 来约束对象 然后把记录的约束对象干掉 最后再去更新约束
                
                //3.更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    //1.记录约束对象
                    self.bottomConstraint = make.bottom.equalTo(textFile.snp_bottom)/*.offset(cellIconMargin)*/.constraint
                })
                
            }
        }
    }
    
    //入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpViews()
        
        //给图片视图添加背景色
        let numColor: CGFloat = CGFloat(random() % 50) * 0.01 + 40
        pictureView.backgroundColor = UIColor(white:numColor, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置子视图
    func setUpViews() {
        addSubview(iconImage)
        addSubview(nameLabel)
        addSubview(vipImage)
        addSubview(verifiImage)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(textFile)
        addSubview(pictureView)
        //分割线
        addSubview(spelineView)
        
        spelineView.backgroundColor = UIColor(white: 0.64, alpha: 1.0)
        
        spelineView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.height.equalTo(6.4)
        }
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(spelineView.snp_bottom).offset(cellIconMargin)
            make.left.equalTo(self).offset(cellIconMargin)
            make.size.equalTo(CGSize(width: cellIconWidth, height: cellIconWidth))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(cellIconMargin)
        }
        
        vipImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(cellIconMargin)
        }
        
        verifiImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImage.snp_right).offset(-Double(cellIconMargin) * 0.5)
            make.centerY.equalTo(iconImage.snp_bottom).offset(-Double(cellIconMargin) * 0.5)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImage.snp_right).offset(cellIconMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(cellIconMargin)
            
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(cellIconMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        textFile.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(cellIconMargin)
            make.top.equalTo(iconImage.snp_bottom).offset(cellIconMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(cellIconMargin)
            make.top.equalTo(textFile.snp_bottom).offset(cellIconMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            //1.记录约束对象
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(cellIconMargin).constraint
        }
        
    }
    
    //加载子视图
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel: UILabel = UILabel(tText: "", tColor: themeColor, tFont: 14)
    private lazy var vipImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    private lazy var verifiImage: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var timeLabel: UILabel = UILabel(tText: "", tColor: themeColor, tFont: 10)
    private lazy var sourceLabel: UILabel = UILabel(tText: "", tColor: UIColor.grayColor(), tFont: 10)
    private lazy var textFile: UILabel = UILabel(tText: "", tColor: UIColor(white: 0.12, alpha: 1.0) , tFont: 14, isNum: 12)
    
    //加载子视图- 图片视图
    private lazy var pictureView: MKPictureView = MKPictureView()
    
    //加载子视图- 每一个cell的分割线
    private lazy var spelineView: UIView = UIView()
}